package cn.edu.hit.education.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import cn.edu.hit.education.dialog.CourseDialog;
import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.view.CourseExploreView;
import educationschedule.Activator;
import educationschedule.Application;

public class UpdateCourseAction extends Action {
	private CourseExploreView viewPart;
	ICourseService courseService = (ICourseService)Application.applicationContext.getBean("courseServiceImpl");
	private int courseId;
	
	public UpdateCourseAction(CourseExploreView viewPart,int courseId){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/toolbar/course_code_16px.png"));
		this.setToolTipText("修改课程");
		this.setText("修改课程");
		this.viewPart = viewPart;
		this.courseId = courseId;
	}
	
	@Override
	public void run() {
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		CourseDialog courseDialog = new CourseDialog(parentShell);
		Course course = courseService.queryCourseByPrimaryKey(courseId);
		courseDialog.setCourse(course);
		if(IDialogConstants.OK_ID == courseDialog.open()){
			int count = courseService.update(course);
			if(count > 0){
				viewPart.refreshUpdateData(course);
			}
		}
		super.run();
	}

	

}
